<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">图片管理</a>
          <a><cite>首页轮播图管理</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body ">
        <button class="layui-btn layui-btn-danger layui-btn-sm delAll"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn layui-btn-sm" onclick="parent.xadmin.add_tab('添加轮播图','/admin/banner/add')"><i
                class="layui-icon"></i>添加
        </button>
    </div>
    <div class="layui-card-body ">
        <table class="layui-hide" id="test" lay-filter="demo"></table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>
</body>
<script>
    layui.use(['table', 'jquery', 'form', 'element'], function () {
        var table = layui.table
        $ = layui.jquery,
            form = layui.form;
        layui.element.render();
        table.render({
            elem: '#test'
            , url: '/admin/banner/tree'
            , cellMinWidth: 70
            , cols: [
                [
                    {type: 'checkbox'}
                    , {field: 'id', title: 'ID', width: 90, unresize: true, sort: true}
                    , {field: 'title', title: 'banner标题'}
                    , {field: 'sorts', title: '排序'}
                    , {field: 'type', title: '类型'}
                    , {
                    field: 'cover', title: '封面图', align: 'center', height: 150
                    , templet: function (d) {
                        return "<img src=\"" + d.cover + "\"></img>";
                    }
                }
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]
            ]
            , page: true
        });
        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'detail':
                    layer.msg('ID：' + data.id + ' 的查看操作');
                    break;
                case 'del':
                    layer.confirm('确定要删除该斑斓图吗？', function (index) {
                        $.ajax({
                            url: '/admin/banner/delete',
                            method: 'post',
                            data: {id: data.id},
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == 0) {
                                    obj.del();
                                    layer.msg('删除成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 5});
                                }
                            },
                            error: function (data) {
                                layer.msg('服务器繁忙', {icon: 5});
                            }
                        });
                        layer.close(index);
                    });
                    break;
                case 'edit':
                    xadmin.open('编辑', '/admin/banner/edit?id=' + data.id);
                    // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                    break;
                default:
                    break;
            }
        });

        $('.delAll').on('click', function () {
            var checkStatus = table.checkStatus('test')
                , data = checkStatus.data
                , ids = [];
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                layer.confirm('确定要删除' + data.length + '个斑斓图吗？', function (index) {
                    $.ajax({
                        url: '/admin/banner/deleteAll',
                        method: 'post',
                        data: {ids: ids},
                        success: function (res) {
                            if (res.code == 0) {
                                table.reload('test');
                                layer.msg('删除成功', {icon: 1});
                            } else {
                                layer.msg(res.msg, {icon: 5});
                            }
                        },
                        error: function (data) {
                            layer.msg('服务器繁忙', {icon: 5});
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
</html>